﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
//参考自：https://www.cnblogs.com/fengxinyuan/p/16908940.html
namespace FieldTools.MediaFindOut.Control
{
    /// <summary>
    /// 消息通知类
    /// </summary>
    public class MessageManage
    {
        internal static Dictionary<string, MessageHost> MessageHosts { get; set; } = MessageHosts ?? [];
        const string defaultToken = "CustomWindowMessageToken";
        /// <summary>
        /// 默认绑定自定义窗口样式
        /// </summary>
        static public string DefaultToken => defaultToken;
        /// <summary>
        /// 默认
        /// </summary>
        /// <param name="message"></param>
        /// <param name="token"></param>
        /// <param name="time"></param>
        public static void Default(string message, string token = defaultToken, int time = 3000)
        {
            Show(message, MessageType.Default, token, time);
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="message"></param>
        /// <param name="token"></param>
        /// <param name="time"></param>
        public static void Success(string message, string token = defaultToken, int time = 3000)
        {
            Show(message, MessageType.Success, token, time);
        }
        /// <summary>
        /// 警告
        /// </summary>
        /// <param name="message"></param>
        /// <param name="token"></param>
        /// <param name="time"></param>
        public static void Warning(string message, string token = defaultToken, int time = 3000)
        {
            Show(message, MessageType.Warning, token, time);
        }
        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="message"></param>
        /// <param name="token"></param>
        /// <param name="time"></param>
        public static void Error(string message, string token = defaultToken, int time = 3000)
        {
            Show(message, MessageType.Error, token, time);
        }

        private static void Show(string message, MessageType messageType, string token, int time)
        {
            if (!MessageHosts.TryGetValue(token, out MessageHost? value)) return;
            var messageHost = value;
            messageHost.Dispatcher.BeginInvoke(() =>
            {
                messageHost.Items.Add(new Message() { MessageType = messageType, Content = message, Time = time, Uid = Guid.NewGuid().ToString() });
            });
        }


    }
    internal class Message : ContentControl
    {
        public int Time { get; set; }

        [Bindable(true)]
        public MessageType MessageType { get { return (MessageType)GetValue(MessageTypeProperty); } set { SetValue(MessageTypeProperty, value); } }
        public static readonly DependencyProperty MessageTypeProperty = DependencyProperty.Register("MessageType", typeof(MessageType), typeof(Message), new PropertyMetadata(default(MessageType)));

        internal Message()
        {
            Loaded += Message_Loaded;
        }

        private async void Message_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (Parent is MessageHost host)
            {
                await Task.Delay(Time);
                host.Items.Remove(this);
            }
        }
    }
    /// <summary>
    /// 消息类型枚举
    /// </summary>
    public enum MessageType
    {
        /// <summary>
        /// 默认
        /// </summary>
        [Description("默认")]
        Default,
        /// <summary>
        /// 成功
        /// </summary>
        [Description("成功")]
        Success,
        /// <summary>
        /// 警告
        /// </summary>
        [Description("警告")]
        Warning,
        /// <summary>
        /// 错误
        /// </summary>
        [Description("错误")]
        Error
    }

    /// <summary>
    /// 消息提示控件容器
    /// </summary>
    public class MessageHost : ItemsControl
    {
        /// <summary>
        /// 消息token
        /// </summary>
        public string Token { get { return (string)GetValue(TokenProperty); } set { SetValue(TokenProperty, value); } }
        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty TokenProperty = DependencyProperty.Register("Token", typeof(string), typeof(MessageHost), new PropertyMetadata(OnTokenChanged));

        private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is MessageHost host)
            {
                var token = e.NewValue.ToString();
                if (!string.IsNullOrEmpty(token))
                {
                    _ = MessageManage.MessageHosts.Remove(token);
                    MessageManage.MessageHosts.Add(token, host);
                }
            }
        }
        /// <inheritdoc/>
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is Message;
        }
        /// <inheritdoc/>
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new Message();
        }
    }

}
